package com.project.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("cookie")
public class CookieController {

    @RequestMapping("test")
    public String test(HttpServletResponse response){
        //创建cookie对象，附加cookie信息
        Cookie c1 = new Cookie("name","tom");
        Cookie c2 = new Cookie("id","008");
        //设置cookie过期时间
        c2.setMaxAge(200000);

        //将cookie信息发送给客户端
        response.addCookie(c1);
        response.addCookie(c2);

        return "ok";
    }

    @RequestMapping("mytest")
    public String mytest(HttpServletRequest request){
        //得到客户端提交的cookie信息
        Cookie[] cookieArray = request.getCookies();
        for(Cookie c : cookieArray){
            System.out.println(c.getName()+"   "+c.getValue());
        }
        return "hello";
    }
}
